package com.smarth.zhizulian.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smarth.zhizulian.R;
import com.smarth.zhizulian.adapter.MyDiscipleAdapter;
import com.smarth.zhizulian.adapter.NewMyDiscipleAdapter;
import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.app.EventFragment;
import com.smarth.zhizulian.bean.Disciple;
import com.smarth.zhizulian.bean.Page;
import com.smarth.zhizulian.bean.disciple.DiscipleDisNum;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.http.KumaHttp;
import com.smarth.zhizulian.http.observer.ProgressObserver;
import com.smarth.zhizulian.http.observer.inter.IRefreshObserver;
import com.smarth.zhizulian.http.utils.RxCompos;
import com.smarth.zhizulian.utils.listhelper.impl.model.HttpListDataModel;
import com.smarth.zhizulian.utils.listhelper.impl.model.ListDataModel;
import com.smarth.zhizulian.utils.listhelper.inter.IDataAdapter;
import com.smarth.zhizulian.utils.listhelper.inter.IDataModel;
import com.smarth.zhizulian.utils.listhelper.RefreshHelper;
import com.smarth.zhizulian.utils.listhelper.inter.ResponseSender;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 18865 on 2017/12/25.
 */

public class MyDiscipleFragment extends EventFragment {
    @BindView(R.id.lv_listview)
    ListView lvListview;
    @BindView(R.id.rv_refresh)
    SmartRefreshLayout rvRefresh;
    @BindView(R.id.ll_loadinglayout)
    LoadingLayout llLoadinglayout;
    private List<Disciple> mData = new ArrayList<>();
    private RefreshHelper<List<Disciple>> refreshHelper;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setBKView(R.layout.fragment_myapprentice);
        initList();
    }

    @Override
    protected void onLogin(EventBean eb) {
        refreshHelper.refresh();
    }

    private void initList() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.header_disciple, null);
        final TextView tv_disciplenum = view.findViewById(R.id.tv_disciplenum);
        final TextView tv_disciplenum_good = view.findViewById(R.id.tv_disciplenum_good);
        final ImageView iv_sowhat = view.findViewById(R.id.iv_sowhat);
        view.findViewById(R.id.iv_what).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Observable.timer(3000, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                iv_sowhat.setVisibility(View.VISIBLE);
                                v.setClickable(false);
                            }
                        })
                        .subscribe(new Consumer<Long>() {
                            @Override
                            public void accept(Long aLong) throws Exception {
                                iv_sowhat.setVisibility(View.GONE);
                                v.setClickable(true);
                            }
                        });
            }
        });
        lvListview.addHeaderView(view);
        refreshHelper = new RefreshHelper<List<Disciple>>(mContext, llLoadinglayout, rvRefresh);
        refreshHelper.setDataModel(new HttpListDataModel<List<Disciple>>(mContext) {
            @Override
            public Observable<? extends Page> getObservable(ResponseSender<List<Disciple>> responseSender, int page) {
                KumaHttp.getService().discipleDisNum().compose(RxCompos.<DiscipleDisNum>getBECompos())
                        .subscribe(new ProgressObserver<DiscipleDisNum>(mContext, false) {
                            @Override
                            public void onNext(DiscipleDisNum discipleDisNum) {
                                tv_disciplenum.setText("" + discipleDisNum.getAllcount());
                                tv_disciplenum_good.setText("" + discipleDisNum.getActivecount());
                            }
                        });
                return KumaHttp.getService().disciplePage(page)
                        .compose(RxCompos.<Page<Disciple>>getBELoginCompos());
            }
        });
        refreshHelper.setDataAdapter(new TestDicipleAdapter(mContext, mData));
        refreshHelper.setOnRetryEmptyListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventBean(BaseConfig.EventCode.SHOW_INVITEDICIPLE), "InviteFragment");
            }
        });
        refreshHelper.refresh();
    }

    private class TestDicipleAdapter extends NewMyDiscipleAdapter implements IDataAdapter<List<Disciple>> {

        public TestDicipleAdapter(Context context, List<?> datas) {
            super(context, datas);
        }

        @Override
        public void onGetData(List<Disciple> disciples, boolean isRefresh) {
            if (isRefresh) {
                mDatas.clear();
            }
            mDatas.addAll(disciples);
            this.notifyDataSetChanged();
        }

        @Override
        public boolean isDataEmpty() {
            return mDatas == null || mDatas.size() == 0;
        }

        @Override
        public List<Disciple> getMyData() {
            return (List<Disciple>) mDatas;
        }
    }

}
